#  This file is part of codeogre
#  Copyright (C) 2009 Johan Jordaan (www.johanjordaan.co.za)
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
sys.path.append('..\..')
import unittest

import codeogre.tokeniser

def print_tokens(tokens):
  print [(t.name,t.text) for t in tokens]

class TokeniserTestCases(unittest.TestCase):

  def setUp(self):
    pass

  def test_simple_1(self):
    tokens = codeogre.tokeniser.tokenise("112 2 3",[[None,'\s'],['NUMBER','\d+']])    
    self.assertEqual(3, len(tokens))

  def test_simple_2(self):
    tokens = codeogre.tokeniser.tokenise("1+ 2- 3",[[None,'\s'],['NUMBER','\d+']])    
    self.assertEqual(5, len(tokens))

  def test_simple_3(self):
    tokens = codeogre.tokeniser.tokenise(" 1+2 ",[[None,'\s'],['NUMBER','\d+']])    
    self.assertEqual(3, len(tokens))
    
  def test_simple_4(self):
    tokens = codeogre.tokeniser.tokenise("1 2 3 XXX Y",[[None,'\s'],['NUMBER','\d+']])    
    self.assertEqual(5, len(tokens))
    self.assertEqual('XXX',tokens[-2].text)
    self.assertEqual('XXX',tokens[-2].name)
    self.assertEqual('Y',tokens[-1].text)
    self.assertEqual('Y',tokens[-1].name)

  def test_simple_5(self):
    tokens = codeogre.tokeniser.tokenise("/* This is single line a comment */ 1+2",[['COMMENT','/\*.*?\*/'],['NUMBER','\d+'],[None,'\s']])    
    self.assertEqual(4, len(tokens))
    self.assertEqual('COMMENT',tokens[0].name)

  def test_simple_6(self):
    tokens = codeogre.tokeniser.tokenise("/* This is multi \n line a comment */ 1+2",[['COMMENT','/\*.*?\*/'],['NUMBER','\d+'],[None,'\s']])    
    self.assertEqual(4, len(tokens))
    self.assertEqual('COMMENT',tokens[0].name)

  def test_simple_7(self):
    tokens = codeogre.tokeniser.tokenise("",[['COMMENT','/\*.*?\*/'],['NUMBER','\d+'],[None,'\s']])    
    self.assertEqual(0, len(tokens))

  def test_simple_8(self):
    tokens = codeogre.tokeniser.tokenise('"This is a string"',[['STRING','".*?"']])    
    self.assertEqual(1, len(tokens))
    self.assertEqual('"This is a string"',tokens[0].text)
    
  def test_simple_9(self):
    def t(tokeniser):
      m = tokeniser.current_match
      if m[-1] == '\\':
        return False
      else:
        trailing_c = tokeniser.input[tokeniser.input_index]
        tokeniser.input_index += 1
        tokeniser.current_match += trailing_c
        return True
      
    tokens = codeogre.tokeniser.tokenise('"This is\\" a \\"string" +',[['STRING','\"[^\"]*',t ],[None,'\s']])    
    self.assertEqual(2, len(tokens))
    self.assertEqual('"This is\\" a \\"string"',tokens[0].text)
    self.assertEqual('+',tokens[1].text)

    
suite = unittest.makeSuite(TokeniserTestCases)
          
if __name__ == '__main__':
  unittest.main()
